package org.dandsoft.blackbox.util.list;

/**
 * User: dvilyuzhanin
 * Date: 20.01.2009 0:58:41
 */
public class Entry<Type> {
    private Type object;
    private Entry<Type> next;
    private Entry<Type> previous;

    public Entry(Type object) {
        if (object != null) {
            this.object = object;
        } else {
            throw new NullPointerException("Object can not be null");
        }
    }

    protected synchronized void connectAfter(Entry<Type> entry) {
        synchronized (entry) {
            entry.setPrevious(this);
            entry.setNext(next);
            if (next != null) {
                next.setPrevious(entry);
            }
            next = entry;
        }
    }

    protected synchronized void disconnect() {
        if (next != null) {
            next.setPrevious(previous);
            if (previous != null) {
                previous.setNext(next);
            }
        }
        if (previous != null) {
            previous.setNext(next);
            if (next != null) {
                next.setPrevious(previous);
            }
        }

        next = null;
        previous = null;
    }

    public Type getObject() {
        return object;
    }

    public synchronized Entry<Type> getNext() {
        return next;
    }

    public synchronized Entry<Type> getPrevious() {
        return previous;
    }

    protected synchronized void setNext(Entry<Type> next) {
        this.next = next;
    }

    protected synchronized void setPrevious(Entry<Type> previous) {
        this.previous = previous;
    }
}
